import axios from 'axios';
import { Message } from 'element-ui';
import router from '../router/index';


axios.defaults.withCredentials = true;
if (process.env.NODE_ENV == 'production') {
    axios.defaults.baseURL = 'https://'+window.location.host;
} else {
    axios.defaults.baseURL = 'https://course.mingshix.com/';
}
// console.log(process.env.NODE_ENV,axios.defaults.baseURL)


//请求拦截器
axios.interceptors.request.use(
    config => {
        // console.log(config)
        return config;
    },
    error => {
        console.log('配置失败')
        return Promise.error(error);
    })

axios.interceptors.response.use(
    response => {
        // console.log(response.data,'*****************************')
        if (response.status === 200) {
            if (response.data.code === 403) {
                Message.error('登录过期，请重新登录')
                setTimeout(() => {
                    router.replace({
                        path: '/',
                    });
                }, 1000);
                sessionStorage.clear();
            }
            return Promise.resolve(response);
        } else {
            console.log('1')
            return Promise.reject(response);
        }
    },
    error => {
        console.log(error)
        Message({
            message: '服务器错误',
            type: 'error'
        })
        return Promise.reject(error) //接口500抛出异常（不走页面逻辑）
    }
);

export default axios;